package com.example.mqexamples.custom.mq;

import org.springframework.util.StringUtils;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @Description TODO
 * @Author benjamin
 * @Date 2020/1/21 9:43
 * @Version
 */
public class Broker {

    private final static int MAX_SIZE = 3;

    private static ArrayBlockingQueue<String> messageQueue = new ArrayBlockingQueue<>(MAX_SIZE);

    public static void produce(String msg) {

        if (messageQueue.offer(msg)) {
            System.out.println("成功向消息中心投递消息： " + msg + "； 当前的消息数量是： " + messageQueue.size());
        } else {
            System.out.println("消息处理中心已达最大负荷，投递消息失败");
        }

        System.out.println("========================");
    }


    public static String consume() {
        String msg = messageQueue.poll();

        if (!StringUtils.isEmpty(msg)) {
            System.out.println("消费消息： " + msg + ", 当前消息中心消息数量： " + messageQueue.size());
        } else {

            System.out.println("没有消息了");
        }
        System.out.println("========================");


        return msg;
    }


}
